package com.neusoft.elm.service;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.dao.OrdersDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.po.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrderDetailetDao orderDetailetDao;
    @Autowired
    private BusinessDao businessDao;
    @Autowired
    private CartDao cartDao;
    @Transactional(rollbackFor = Exception.class )
    @Override
    public int createOrders(String userId, Integer businessId, Integer daId) throws Exception {
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        orders.setOrderDate(format);
        double total=0.0;
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setBusinessId(businessId);

        List<Cart> carts = cartDao.listCart(cart);
        for (Cart c : carts) {
            total+=c.getFood().getFoodPrice()*c.getQuantity();
        }
        Business business = businessDao.getBusinessById(businessId);
        total+= business.getDeliveryPrice();
        orders.setOrderTotal(total);
//        保存订单获得订单id
        int i = ordersDao.saveOrders(orders);
        if (i!=1){
            throw new Exception("添加订单失败");
        }
        int orderId=orders.getOrderId();
        for (Cart c : carts) {
            OrderDetailet orderDetailet=new OrderDetailet();
            orderDetailet.setOrderId(orderId);
            orderDetailet.setFoodId(c.getFoodId());
            orderDetailet.setQuantity(c.getQuantity());
            orderDetailetDao.saveOrderDetailet(orderDetailet);


        }
//        清空购物车中对应的记录
        cartDao.removeCart(cart);

        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = ordersDao.getOrdersById(orderId);
        Business business = businessDao.getBusinessById(orders.getBusinessId());
        orders.setBusiness(business);
        List<OrderDetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
        orders.setOdList(list);
        return orders;
        
    }


    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
      List<Orders>orders=ordersDao.listOrdersById(userId);
        for (Orders order : orders) {
            order.setBusiness(businessDao.getBusinessById(order.getBusinessId()));
        order.setOdList(orderDetailetDao.listOrderDetailetByOrderId(order.getOrderId()));

        }
        return orders;
    }
}
